package com.xm.xmapp.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.PileOrderDetailActivity;
import com.xm.xmapp.PileUsingOrderDetailActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.adapter.PileOrderListAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.ListViewOnclickInterFace;
import com.xm.xmapp.request.bean.Get_Pile_Order_ListRequest;
import com.xm.xmapp.response.bean.ResponsePileOrderListBean;
import com.xm.xmapp.response.bean.ResponsePileOrderList;

public class PileOrderFragment extends BaseFragment {

	private XListView xListView;
	private RelativeLayout rel;
	private Get_Pile_Order_ListRequest requestBean;
	private ArrayList<ResponsePileOrderList> list;
	private PileOrderListAdapter adapter;
	private int page;

	public PileOrderFragment() {

	}

	@Override
	public int setContentView() {
		return R.layout.fragment_order;
	}

	@Override
	public void initView() {
		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
	}

	@Override
	public void initData() {

		requestBean = new Get_Pile_Order_ListRequest();
		page = 1;
		requestBean.pagesize = CodeConstant.pageSize;
		list = new ArrayList<ResponsePileOrderList>();
		adapter = new PileOrderListAdapter(list, getActivity(), R.layout.item_pile_order);
		adapter.setIn(new ListViewOnclickInterFace() {

			@Override
			public Object DoSomeThing(Object data) {
				ResponsePileOrderList t = (ResponsePileOrderList) data;
				Intent intent;
				if (t.charging_order_status == 2) {
					intent = new Intent(MyAppcation.getMyAppcation(), PileUsingOrderDetailActivity.class);
				} else {
					intent = new Intent(MyAppcation.getMyAppcation(), PileOrderDetailActivity.class);
					if (t.order_status == 0)
						intent.putExtra("status", true);
				}
				intent.putExtra("orderId", t.charging_order_no);
				startActivity(intent);
				return null;
			}
		});

		xListView.setAdapter(adapter);
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);

		promptDialog.show();
		getData();

	}

	private void getData() {

		requestBean.page = page;
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				xListView.stopRefresh();
				xListView.stopLoadMore();
				promptDialog.dismiss();

				ResponsePileOrderListBean base = JSON.parseObject(response, ResponsePileOrderListBean.class);
				if (page == 1) {
					list.clear();
				}
				list.addAll(base.list);
				if (base.list == null || base.list.size() == 0) {
					xListView.setPullLoadEnable(false);
				}

				if (list.size() > 0) {
					xListView.setVisibility(View.VISIBLE);
					rel.setVisibility(View.GONE);
				} else {
					xListView.setVisibility(View.GONE);
					rel.setVisibility(View.VISIBLE);
				}
				adapter.notifyDataSetChanged();

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
				xListView.stopRefresh();
				xListView.stopLoadMore();
			}
		});

	}

	@Override
	public void initEvent() {
		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				xListView.setPullLoadEnable(true);
				page = 1;
				getData();
			}

			@Override
			public void onLoadMore() {

				page++;
				getData();
			}
		});
	}

	/**
	 * 去支付成功
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case Type.F_PileOrder.DEFAULT:
			page = 1;
			getData();
			break;

		default:
			break;
		}

		return super.dataChang(flag, data);

	}
}
